package com.bianguo.android.pigeondemo.view.activity;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.bianguo.android.pigeondemo.Presenter.BasePresenter;
import com.bianguo.android.pigeondemo.model.BaseModel;
import com.bianguo.android.pigeondemo.utils.PermissionHelper;
import com.bianguo.android.pigeondemo.view.IBaseView;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/4.
 */

public abstract class BaseActivity<V extends IBaseView, M extends BaseModel, P extends BasePresenter<V, M>> extends AppCompatActivity {

    protected P mPresenter;
    protected Activity mContext;
    private Unbinder unBind;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(layoutId());
        boolean permission = PermissionHelper.getInstance(this).isPermission(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PermissionHelper.PERMISSION_WRITE);
        if (permission) {
            mPresenter = initPresenter();
        }
        unBind = ButterKnife.bind(this);
        initView(savedInstanceState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PermissionHelper.PERMISSION_WRITE:
                if (PackageManager.PERMISSION_GRANTED != grantResults[0]) {

                } else {
                    mPresenter = initPresenter();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unBind.unbind();
        if (mPresenter != null) {
            mPresenter.detach();
        }
    }

    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    public abstract int layoutId();

    public abstract P initPresenter();

    public abstract void initView(@Nullable Bundle savedInstanceState);
}
